<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use think\Controller;
use app\admin\model\Carts;
use app\admin\model\Networks;
class Cart extends Common{
	public function _initialize()
    {
        parent::_initialize();
        $this->Carts = new Carts;
        
    }
    public function delete($_id=null){
    	if($_id > 0 && $this->Carts->where('id',$_id)->delete()){
    		return ['code'=>1,'msg'=>lang('The line has been removed.')];
    	}
    	return ['code'=>0,'msg'=>lang('The line removal failed.')];
    }
    /*提交到数据库的*/
    public function confirm(){
    	$carts =db('carts')->where('user_id',$this->User_id)->select();
    	if($carts){
    		$base = [];
    		foreach($carts as $cart){
    			if(empty($cart['value'])){
    				continue;
    			}
    			$Network = new Networks;
    			$data = unserialize($cart['value']);
    			$result = $Network->data($data)->allowField(true)->save();
    			if($result){
    				db('network_logs')->insert(
	                    array(
	                        'user_id'=>$this->User_id,
	                        'network_id'=>$Network->id,
	                        'status'=>$data['status'],
	                        'content'=>lang('Apply for network lines'),
	                        'create_time'=>time()
	                    )
	                );
	                $data['surname'] = $this->user['email'];
	                $data['create_time'] = datetimeFormat($data['create_time']);
                    $data['id'] = $Network->id;
	                $base[] = $data;
    			}
    		}
    		/*发送消息*/
    		if(count($base) > 0){
    			$this->notice(array('status'=>18,'networks'=>$base));
    		}
    		$this->Carts->where(['user_id'=>$this->User_id])->delete();//删除购物车里面的
    		return ['code'=>1,'msg'=>lang('The line has been submitted for application.'),'url'=>U('network/index')];
    		
    	}
    	return ['code'=>0,'msg'=>lang('The line submission failed.')];
    }
    function getList(){
        $carts =db('carts')->where('user_id',$this->User_id)->select();
        $data = [];
        if($carts){
            
            foreach($carts as $cart){
                $b = unserialize($cart['value']);
                $b['_id'] = $cart['id'];
                $data[] = $b;
            }

        }
        return $result = ['code'=>0,'msg'=>lang('No Data...'),'data'=>$data,'count'=>count($data),'rel'=>1];
    }
    public function getData($_id=null){
        $cart =db('carts')->where('user_id',$this->User_id)->where('id',$_id)->find();
        if($cart){
            return ['code'=>1,'msg'=>'','data'=>unserialize($cart['value'])];
        }
        return ['code'=>0,'msg'=>lang('No Data...')];
    }
    public function add(){
        if($this->request->isPost()){
            $data = $this->request->post();
            //Carts::deleteAll($this->User_id); //删除
            if((int)$data['type'] == 3){
                $data['country_id'] = (int)$data['cloud_country'];
                $data['city_id'] = (int)$data['cloud_city'];
            }
            if($data['endtype'] == 3){
                $data['ecountry_id'] = (int)$data['endcloudaddrid'];
                $data['ecity_id'] = (int)$data['ecloud_city'];
            }
            $result = Carts::addData($this->User_id,$data); //加入购物车
            if($result){
                return ['code'=>1,'msg'=>'success','url'=>U('network/edit',['cat_id'=>$result])];
            }
        }
        return ['code'=>0,'msg'=>'error'];
    }
}